/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epcs.model.dao.outlookWrapper;

import java.io.IOException;
import java.net.SocketException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;

/**
 *
 * @author Hà Hải Đăng
 */
public class WebUtil {

    public static String getResult(String url, String accessToken) {
        try {
            return Request.Get(url).setHeader("Accept-Charset", "UTF-8")
            		.connectTimeout(60000).socketTimeout(180000)
                    .setHeader("Authorization", "Bearer " + accessToken)
                    .execute().returnContent().asString();
        } catch (HttpResponseException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClientProtocolException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SocketException ex) {
        	Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public static String deleteResult(String url, String accessToken) {
        try {
            return Request.Delete(url).setHeader("Accept-Charset", "UTF-8")
                    .setHeader("Authorization", "Bearer " + accessToken)
                    .connectTimeout(60000).socketTimeout(180000)
                    .execute().returnContent().asString();
        } catch (HttpResponseException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClientProtocolException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SocketException ex) {
        	Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public static String postResult(String url, String accessToken, String json) {
        try {
            return Request.Post(url).addHeader("Authorization", "Bearer " + accessToken)
            		.connectTimeout(60000).socketTimeout(180000)
                    .bodyString(json, ContentType.APPLICATION_JSON)
                    .execute().returnContent().asString();
        } catch (HttpResponseException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClientProtocolException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SocketException ex) {
        	Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public static String postResult(String url, List<NameValuePair> form) {
        try {
            return Request.Post(url).bodyForm(form)
            		.connectTimeout(60000).socketTimeout(180000)
                    .execute().returnContent().asString();
        } catch (HttpResponseException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClientProtocolException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SocketException ex) {
        	Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public static String putResult(String url, String accessToken, String json) {
        try {
            return Request.Put(url).addHeader("Authorization", "Bearer " + accessToken)
            		.connectTimeout(60000).socketTimeout(180000)
                    .bodyString(json, ContentType.APPLICATION_JSON)
                    .execute().returnContent().asString();
        } catch (HttpResponseException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClientProtocolException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SocketException ex) {
        	Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WebUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }
}
